

#import "XBFeatureViewController.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "XBSaveDataTool.h"

@interface XBFeatureViewController ()<UIScrollViewDelegate>

// 分页控制器
@property(nonatomic,weak) UIPageControl *pageControl;

// 图片数量
@property (nonatomic,assign) NSInteger imageCount;

@property(nonatomic,weak) XBFeatureViewController *featureVc;
@property (nonatomic,weak) UIImageView *imageView;
@end

@implementation XBFeatureViewController
#pragma mark - ======== 登录方法 ========
- (void)loginApplication:(UIApplication *)application rootVc:(void (^)(__kindof UIViewController *viewController))rootViewController
{
    // 显示状态栏
    application.statusBarHidden = NO;
    application.statusBarStyle = UIStatusBarStyleLightContent;
    
    // 保存版本号使用的key
    NSString *versionKey =  (__bridge NSString *) kCFBundleVersionKey;
    // core Foundataion --- Foundation 可以相互转换
    // CFStringRef--- NSString
    // CFArrayRef --- NSArray
    //
    // 获得上一次保存的版本号
    NSString *lastVersion = [XBSaveDataTool valueForKey:versionKey]; // 1.2
    // 获得当前使用的版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[versionKey]; // 1.2
    
    // 比较两个版本号是否相同
    if(![lastVersion isEqualToString:currentVersion]) {
        
        // 把新的版本号覆盖旧点版本号
        [XBSaveDataTool saveValue:currentVersion forKey:versionKey]; // 1.2
        rootViewController(self);
    } else {
        
        if (self.mainStoryBoard == nil) {
            self.mainStoryBoard = @"Main";
        }
        // 显示主界面
        UIStoryboard *sb = [UIStoryboard storyboardWithName:self.mainStoryBoard bundle:nil];
        // 创建箭头指向的控制器
        UIViewController *vc = [sb instantiateInitialViewController];
        rootViewController(vc);
    }
}

#pragma mark - ======== 赋值属性 ========
- (void)setImages:(NSArray *)images
{
    _images = images;
    self.imageCount = images.count;

    [self setupScrollView];
}

- (void)setImageURL:(NSArray *)imageURL
{
    _imageURL = imageURL;
    self.imageCount = imageURL.count;

    [self setupScrollView];
}
- (void)setPageControlHidden:(BOOL)pageControlHidden
{
    _pageControlHidden = pageControlHidden;
    if (!pageControlHidden) {
        // 创建分页指示器
        [self setupPageControl];
    }
}
- (void)setStartImageName:(NSString *)startImageName
{
    _startImageName = startImageName;
    [self setupStartButton:self.imageView];
}

#pragma mark - ======== view Didload ========
- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - ======== 加载控件 ========
/**
 *  创建scrollView
 */
- (void)setupScrollView{

    // 创建scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    // 设置代理
    scrollView.delegate = self;
    // 取消弹簧效果
    scrollView.bounces = NO;
    // 设置分页
    scrollView.pagingEnabled = YES;
    // 设置frame
    scrollView.frame = self.view.bounds;
    scrollView.showsHorizontalScrollIndicator = NO;
    // 添加控制器的view
    [self.view addSubview:scrollView];
    
    CGFloat imageW = CGRectGetWidth(self.view.frame);
    CGFloat imageH = CGRectGetHeight(self.view.frame);
    
    // 添加图片到scrollView上
    for(NSInteger index = 0;index < self.imageCount; index ++) {
        // 创建图片
        UIImageView *imageView = [[UIImageView alloc] init];
        self.imageView = imageView;
        // 设置frame
        CGFloat imageX = index * imageW;
        CGFloat imageY = 0;
        imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
        
        if (self.images) {
            // 设置图片
            imageView.image = self.images[index];
        }else
        {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageURL[index]]];
        }
        // 将图片添加到scrollView上面
        [scrollView addSubview:imageView];
    }
    // 设置滚动范围
    scrollView.contentSize = CGSizeMake(self.imageCount * imageW, 0);
}

/**
 *  创建分页指示器
 */
- (void)setupPageControl{
    // 创建分页指示器
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    // 设置点个数
    pageControl.numberOfPages = self.imageCount;
    // 设置点颜色
    // 设置当前点的颜色
    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    // 设置其他点的颜色
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    // 设置位置
    pageControl.center = CGPointMake(self.view.center.x, self.view.frame.size.height * 0.95);
    // 添加到控制器的view上面‘
    [self.view addSubview:pageControl];
    
    self.pageControl = pageControl;
}

/**
 *  创建立即体验按钮
 */
- (void)setupStartButton:(UIImageView *)imageView{
    // 设置图片可交互
    imageView.userInteractionEnabled = YES;
    // 创建按钮
    UIButton *startBtn = [[UIButton alloc] init];
    
    if (self.startImageName) {
        // 设置背景
        [startBtn setBackgroundImage:[UIImage imageNamed:self.startImageName] forState:UIControlStateNormal];
    }
    // 设置frame
    startBtn.frame = CGRectMake(0, 0, startBtn.currentBackgroundImage.size.width, startBtn.currentBackgroundImage.size.height);
    startBtn.center = CGPointMake(self.view.center.x, CGRectGetHeight(self.view.frame) - 100);
    // 监听按钮
    [startBtn addTarget:self action:@selector(startBtnClick) forControlEvents:UIControlEventTouchUpInside];
    // 添加到图片上面
    [imageView addSubview:startBtn];
}

#pragma mark - ======== 监听立即体验按钮 ========
/**
 *  监听按钮
 */
- (void)startBtnClick{
    // 获得主窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    self.featureVc = (XBFeatureViewController *)window.rootViewController;
    // 显示主界面
    if (self.mainStoryBoard==nil) {
        self.mainStoryBoard = @"Main";
    }
    UIStoryboard *sb = [UIStoryboard storyboardWithName:self.mainStoryBoard bundle:nil];
    // 创建箭头指向的控制器
    UIViewController *vc = [sb instantiateInitialViewController];
    // 设置窗口的根控制器
    window.rootViewController = vc;
    [window addSubview:self.featureVc.view];
    
    /**
     *  设置消失动画
     */
    [UIView animateWithDuration:0.5 animations:^{
//        CGRect frame = self.featureVc.view.frame;
//        frame.origin.x = -self.featureVc.view.frame.size.width;
//        self.featureVc.view.frame = frame;
        self.featureVc.view.alpha = 0;
    } completion:^(BOOL finished) {

        self.featureVc.view = nil;
    }];
}

#pragma mark - UIScrollViewDelegate 代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 获得x方向偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    // 计算页号
    NSInteger page = (offsetX / scrollView.frame.size.width) + 0.5;
    
    self.pageControl.currentPage = page;
}

- (void)dealloc
{
    NSLog(@"===== 新特性控制器已销毁 =====");
}
@end
